<template>
  <div class="reset-password">
  <div class="demo-ruleForm login-container">
    <h3 class="title">Reset Password</h3>
    <el-form :model="resetEmailForm" ref="resetEmailForm"  :rules="rulesResetEmail">Email: {{resetEmailForm.account}}
      <el-form-item label="Password" prop="password">
        <el-input type="password" v-model="resetEmailForm.password" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="Retype Password" prop="retypePassword">
        <el-input type="password" v-model="resetEmailForm.retypePassword" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item style="margin-bottom:0;">
        <el-row :gutter="30"  type="flex" justify="center" style="padding:20px 0;">
          <el-col :span="12">
          <el-button type="" style="width:100%;" @click="submitEmailForm('resetEmailForm')">Cancel</el-button>
          </el-col>
          <el-col :span="12">
            <el-button type="warning" style="width:100%;" :loading="loading" @click="submitEmailForm('resetEmailForm')">Submit</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </div>
  </div>
</template>

<script>
  import {resetEmailPassword} from '@/api/mgc_api'
  import Qs from 'qs'
  export default {
    name: 'ConfirmEmailPassword',
    data () {
      return {
        loading: false,
        resetEmailForm: {
          account: '',
          password: '',
          retypePassword: ''
        },
        rulesResetEmail: {
          password: [{validator:(rule,value,callback)=>{
              if(/^(?=.*?[0-9]).{8,}$/.test(value) == false){
                callback(new Error("At least 8 or more characters and at least 1 number"));
              }else{
                callback();
              }
            }, trigger:'blur'},
            { required: true, message: 'Please enter password', trigger: 'blur' }
          ],
          retypePassword: [{validator:(rule,value,callback)=>{
            if(value !== this.resetEmailForm.password){
              callback(new Error("The two passwords you typed do not match"));
            }else{
              callback();
            }
          }, trigger:'blur'},
          { required: true, message: 'Please enter password', trigger: 'blur' }
        ],
      },
      }
    },
    methods: {
      // message alert 用于前端错误提示信息
      messageToastErr (msg) {
        this.$message({
          showClose: true,
          message: msg,
          duration: 1500,
          type: 'error'
        })
      },
      submitEmailForm (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let data = {
              'email': this.resetEmailForm.account,
              'password': this.resetEmailForm.password
            }
            let params = Qs.stringify(data)
            this.loading = true
            resetEmailPassword(params)
              .then((res) => {
                if (res.data) {
                  var data = res.data
                  if (data.errorID === 0) {
                    this.showToast(data.errorID)
                    this.$refs[formName].resetFields()
                    this.loading = false
                    this.$router.push('/login')
                  } else {
                    console.log(data)
                    this.loading = false
                    this.showToast(data.errorID)
                  }
                }
              })
              .catch((error) => {
                console.log(error)
                this.loading = false
                this.messageToastErr('error,please try again leter')
              })
          } else {
            this.messageToastErr('error submit!!')
            return false
          }
        })
      }
    }
  }
</script>

<style >
  .reset-password .login-container{
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 0 auto;
    width: 380px;
    padding: 35px 35px 25px 35px;
    background: #fff;
    margin-bottom: 50px;
    box-shadow: none;
    border: none;
  }
</style>
